package com.spring.http;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author Spring King
 * @date 2021/7/10
 */
public class Request {

    private String method;

    private String url;

    private InputStream inputStream;


    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;

        int count = 0;
        while (count == 0) {
            count = inputStream.available();
        }

        byte[] bytes = new byte[count];
        inputStream.read(bytes);

        String inputStr = new String(bytes);
        // 获取请求头第一行信息： GET / HTTP/1.1
        String firstLineStr = inputStr.split("\\n")[0];
        String[] head = firstLineStr.split(" ");

        this.method = head[0];
        this.url = head[1];
    }
}
